package com.tbit.uqbike.webmanager.controller;

import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.uqbike.annos.OperateLog;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.MainCar;
import com.tbit.uqbike.object.pojo.vo.MainCarPageReqVO;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.common.entity.view.PageResult;
import com.tbit.uqbike.service.business.MainCarService;
import com.tbit.uqbike.webmanager.util.Assert;
import com.tbit.utils.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 运维人员车辆管理
 *
 * @author LMY
 * @create 2021-04-07 13:48
 */
@RestController
@RequestMapping("/mainCar")
public class MainCarController {

    @Autowired
    private MainCarService mainCarService;

    /**
     * 插入
     */
    @PostMapping("/insert")
    @OperateLog(module = ModuleEnum.MAINCAR, methods = MethodEnum.MAINCAR_ADD)
    public JsonResult insert(@RequestBody MainCar mainCar) {
        Assert.hasLength(mainCar.getMachineNO(), "Machine.userCode.notNull");
        Assert.hasLength(mainCar.getMainCarNO(), "Machine.machineNO.notNull");
        Assert.notNull(mainCar.getAccountId(), "AccountUser.agentId.notNull");

        MainCar mainCarMachineNO = mainCarService.getByMachineNO(mainCar.getMachineNO());

        if (mainCarMachineNO != null) {
            throw new BaseException("Machine.userCode.exist");
        }

        MainCar mainCarNO = mainCarService.getByMainCarNO(mainCar.getMainCarNO());
        if (mainCarNO != null) {
            throw new BaseException("Machine.machineNO.exist");
        }

        mainCar.setAddTime(DateTimeUtil.getNowTime());

        mainCarService.insert(mainCar);

        return JsonResult.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @OperateLog(module = ModuleEnum.MAINCAR, methods = MethodEnum.MAINCAR_UPDATE)
    public JsonResult update(@RequestBody MainCar mainCar) {
        Assert.hasLength(mainCar.getMainCarNO(), "Machine.userCode.notNull");
        Assert.hasLength(mainCar.getMachineNO(), "Machine.machineNO.notNull");
        Assert.notNull(mainCar.getMainCarId(), "Machine.machineId.notNull");
        Assert.notNull(mainCar.getAccountId(), "AccountUser.agentId.notNull");
        MainCar mainCarNO = mainCarService.getByMainCarNO(mainCar.getMainCarNO());
        if (mainCarNO != null && !mainCarNO.getMainCarId().equals(mainCar.getMainCarId())) {
            throw new BaseException("Machine.userCode.exist");
        }
        MainCar machineNO = mainCarService.getByMachineNO(mainCar.getMachineNO());
        if (machineNO != null && !machineNO.getMainCarId().equals(mainCar.getMainCarId())) {
            throw new BaseException("Machine.machineNO.exist");
        }


        mainCar.setUpdateTime(DateTimeUtil.getNowTime());
        mainCarService.update(mainCar);

        return JsonResult.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/del")
    @OperateLog(module = ModuleEnum.MAINCAR, methods = MethodEnum.MAINCAR_DEL)
    public JsonResult del(Integer mainCarId) {
        Assert.notNull(mainCarId, "Machine.machineId.notNull");

        mainCarService.del(mainCarId);
        return JsonResult.ok();
    }

    /**
     * 根据运营区域查询
     */
    @RequestMapping("/getPage")
    public JsonResult getPage(MainCarPageReqVO reqVO) {
        Assert.notNull(reqVO.getAccountId(), "AccountUser.agentId.notNull");
        PageResult<MainCar> mainCars = mainCarService.getPage(reqVO);
        return JsonResult.ok(mainCars);
    }

    /**
     * 根据车辆id进行查询
     */
    @RequestMapping("/getByCarId")
    public JsonResult getByCarId(Integer mainCarId) {
        MainCar mainCar = mainCarService.getByMainCarId(mainCarId);
        return JsonResult.ok(mainCar);
    }

    /**
     * 根据车辆NO进行查询
     */
    @RequestMapping("/getByCarNO")
    public JsonResult getByCarNO(String mainCarNO) {
        MainCar mainCar = mainCarService.getByMainCarNO(mainCarNO);
        return JsonResult.ok(mainCar);
    }
}
